import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_base/widgets/image/image_view.dart';

class ImageGestureView extends StatefulWidget {
  final String? imageUrl;
  final double? width;
  final double? height;
  final BoxFit? fit;
  final Widget? placeholder;
  final double radius;
  final Color? backgroundColor;
  final Widget? Function(ExtendedImageState)? loadStateHandler;
  final String? imageCacheName;

  const ImageGestureView({
    Key? key,
    this.imageUrl,
    this.width,
    this.height,
    this.fit = BoxFit.cover,
    this.placeholder,
    this.radius = 0.0,
    this.backgroundColor,
    this.loadStateHandler,
    this.imageCacheName,
  }) : super(key: key);

  @override
  State<ImageGestureView> createState() => _ImageGestureViewState();
}

class _ImageGestureViewState extends State<ImageGestureView> with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  Animation? _animation;
  Function() animationListener = () {};

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _animationController = AnimationController(duration: const Duration(milliseconds: 100), vsync: this);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _animation?.removeListener(animationListener);
    _animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return buildImage();
  }

  Widget buildImage() {
    return ImageView(
      imageUrl: widget.imageUrl ?? '',
      width: widget.width?.w,
      height: widget.height?.w,
      fit: widget.fit ?? BoxFit.cover,
      radius: widget.radius,
      enableSlideOutPage: true,
      loadStateHandler: widget.loadStateHandler,
      mode: ExtendedImageMode.gesture,
      onDoubleTap: (state) {
        var pointerDownPosition = state.pointerDownPosition;
        double begin = state.gestureDetails!.totalScale!;
        double end;
        _animation?.removeListener(animationListener);
        _animationController.stop();
        _animationController.reset();
        if (begin == 1) {
          end = 3;
        } else {
          end = 1;
        }
        animationListener = () {
          state.handleDoubleTap(scale: _animation!.value, doubleTapPosition: pointerDownPosition);
        };
        _animation = Tween<double>(begin: begin, end: end).animate(CurvedAnimation(parent: _animationController, curve: Easing.linear));
        _animation!.addListener(animationListener);
        _animationController.forward();
      },
      initGestureConfigHandler: (state) {
        return GestureConfig(
          minScale: 0.9,
          animationMinScale: 0.7,
          maxScale: 3.0,
          animationMaxScale: 3.5,
          speed: 1.0,
          inertialSpeed: 100.0,
          initialScale: 1.0,
          inPageView: true,
          initialAlignment: InitialAlignment.center,
        );
      },
    );
  }
}
